# Contributor: Melven Roehrig-Zoellner <Melven.Roehrig-Zoellner@DLR.de>

_realname=tixi
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.3.0
pkgrel=1
pkgdesc="TiXI is a fast and simple XML interface library (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/DLR-SC/tixi'
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libxslt")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python")
source=("https://github.com/DLR-SC/tixi/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        0001-Dont-build-and-install-the-examples.patch
        0002-Fix-ctypes-DLL-loading-in-Python-3.8-new-policy.patch
        0003-fix-build-with-latest-libxml2.patch::https://github.com/DLR-SC/tixi/commit/732f6ad3.patch)
sha256sums=('988d79ccd53c815d382cff0c244c0bb8e393986377dfb45385792766adf6f6a9'
            'b3411df0057eef3b8fa4c56e432bf90938fedc0833d3cce9d9c66fbcb7101c42'
            '9ef13729efb68420f869e21259bce492ee4c66e624bbe5901abe296f56fafc6a'
            'a87bd7a4ea402bebb298065cd603aa7de291c39b96a2930c30011f88b3740d5f')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -Np1 -i "${srcdir}/0001-Dont-build-and-install-the-examples.patch"
  patch -Np1 -i "${srcdir}/0002-Fix-ctypes-DLL-loading-in-Python-3.8-new-policy.patch"
  dos2unix "${srcdir}"/0003-fix-build-with-latest-libxml2.patch
  patch -Np1 -i "${srcdir}/0003-fix-build-with-latest-libxml2.patch"
}


build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi
  
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
      -DBUILD_SHARED_LIBS=ON \
      "${extra_config[@]}" \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}


package() {
  cd "${srcdir}"/build-${MSYSTEM}

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --build . --target install

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
